package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjUserSet;
import com.zb.mapper.ZjUserSetMapper;
import com.zb.service.ZjUserSetService;
import com.zb.vo.UserSetVo;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 用户设置表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-25
 */
@Service
public class ZjUserSetServiceImpl extends ServiceImpl<ZjUserSetMapper, ZjUserSet> implements ZjUserSetService {

    // 初始化 或 恢复初始化用户设置
    @Override
    public void initOrRestoreDefaultUserSet(Long custAccId) {
        ZjUserSet userSet = new ZjUserSet();
        userSet.setId(custAccId);
        userSet.setCode("US");
        userSet.setSelectKnowledgeAuthor(false);
        userSet.setMobileHasLogin(false);
        userSet.setLevelId(0L);
        this.saveOrUpdate(userSet);
    }

    // 获取用户设置
    @Override
    public UserSetVo getUserSet(Long custAccId) {
        ZjUserSet userSet = this.getById(custAccId);
        return UserSetVo.beanToVo(userSet);
    }

    // 保存用户设置
    @Override
    public void saveUserSet(UserSetVo userSetVo) {
        ZjUserSet userSet = UserSetVo.voToBean(userSetVo);
        this.updateById(userSet);
    }

    // 根据商家ID获取用户编码
    @Override
    public String getCodeById(Long id) {
        QueryWrapper<ZjUserSet> wrapper = new QueryWrapper<>();
        wrapper.select("code");
        wrapper.eq("id", id);
        ZjUserSet set = this.getOne(wrapper);
        return null == set? "US" : set.getCode();
    }

    // 根据商家ID获取等级ID
    @Override
    public Long getLevelIdById(Long id) {
        QueryWrapper<ZjUserSet> wrapper = new QueryWrapper<>();
        wrapper.select("level_id");
        wrapper.eq("id", id);
        ZjUserSet set = this.getOne(wrapper);
        return null == set? 0L : set.getLevelId();
    }

    // 获取客户设置的知识库查看权限
    @Override
    public boolean getSelectKnowledgeAuthor(Long id) {
        QueryWrapper<ZjUserSet> wrapper = new QueryWrapper<>();
        wrapper.select("is_select_knowledge_author AS selectKnowledgeAuthor");
        wrapper.eq("id", id);
        ZjUserSet set = this.getOne(wrapper);
        return null != set && set.getSelectKnowledgeAuthor();
    }

}
